<?php

class RefundController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView()
	{
		if(isset($_GET['id'])){
			$model = Refund::model()->findByPk($_GET['id']);
		}elseif(isset($_GET['zopid'])){
			$model = Refund::model()->findByAttributes(array('zop_id'=>$_GET['zopid']));
		}else{
			throw new CHttpException(404,'The requested page does not exist.');
		}
		
		
		//同意退款
		if(isset($_POST['agree'])){
			$model->Attributes = $_POST['Refund'];
			$model->zr_pay_status = 4;//退款状态为：4退款成功
			$model->zr_cmp_time =time();//更新商家处理时间
			if($model->save()){
				//添加收支明细
				$account = new  MemAccount();
				$account->zm_id = $model->order->zm_id_cmp;//会员ID
				$account->zmc_type = 2;//类型:1增加2.减少
				$account->zmc_event = 5;//'操作类型:1订单2充值3提现4现金券5退款
				$account->zmc_orderid = $model->zr_id;//订单ID/退款流水号
				$account->zmc_amount = $model->zr_amount;//金额
				$account->zmc_note = $model->zr_content;//退款说明
				$account->save();
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		//同意退款，要求买家退货
		if(isset($_POST['returngoods'])){
			$model->zr_pay_status = 3;//退款状态为：3同意退款，要求买家退货
			$model->zr_cmp_time =time();//更新商家处理时间
			if($model->save()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		//拒绝退款
		if(isset($_POST['refuse'])){
			$model->Attributes = $_POST['Refund'];
			$model->zr_pay_status = 2;//退款状态为：2退款失败
			$model->zr_cmp_time =time();//更新商家处理时间
			if($model->save()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		//确答收货并退款
		if(isset($_POST['takeagree'])){
			$model->Attributes = $_POST['Refund'];
			$model->zr_pay_status = 4;//退款状态为：4退款成功
			$model->zr_cmp_time =time();//更新商家处理时间
			if($model->save()){
				//添加收支明细
				$account = new  MemAccount();
				$account->zm_id = $model->order->zm_id_cmp;//会员ID
				$account->zmc_type = 2;//类型:1增加2.减少
				$account->zmc_event = 5;//'操作类型:1订单2充值3提现4现金券5退款
				$account->zmc_orderid = $model->zr_id;//订单ID/退款流水号
				$account->zmc_amount = $model->zr_amount;//金额
				$account->zmc_note = $model->zr_content;//退款说明
				$account->save();
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		//渲染视图
		$this->render('refund_one',array(
			'model'=>$model
		));
	}



	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Refund('search');
		$model->unsetAttributes();  // clear any default values

		//种类1.退款 2.售后维权',
		$model->zr_kind =1;
		
		if(isset($_GET['Refund']))
			$model->attributes=$_GET['Refund'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Refund::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='refund-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * 单条，批量删除
	 */
	public function actionDelete(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zr_id in ($id)");
		//修改内容
		$set = array('zr_del'=>'2');//逻辑删除
		//执行操作
		if(Refund::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
}
